package com.pzr.soccer.service.impl;

import java.time.LocalDate;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.pzr.soccer.model.Role;
import com.pzr.soccer.model.User;
import com.pzr.soccer.service.RoleService;
import com.pzr.soccer.service.UserService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
	@Autowired
	MongoOperations operations;
	@Autowired
	RoleService roleService;

	@Override
	public boolean addRole(String userId, Role role) {
		// TODO Auto-generated method stub

		User user = operations.findById(userId, User.class);
		if (user == null) {
			log.info("no such userId: " + userId);
			return false;
		}

		Role old = roleService.findRoleByUserIdAndName(userId, role.getName());
		if (old != null) {
			operations.remove(old);
			user.getRoleIds().remove(old.getId());
		}

		Role save = operations.save(role);
		user.getRoleIds().add(save.getId());
		operations.save(user);
		
		return true;

	}

	@Override
	public boolean removeRole(String userId, String roleName) {
		// TODO Auto-generated method stub
		User user = operations.findById(userId, User.class);
		if (user == null) {
			log.info("no such userId: " + userId);
			return false;
		}
		Role oldRole = roleService.findRoleByUserIdAndName(userId, roleName);
		if(oldRole==null) {
			log.info("no such role");
			return false ;
		}
		
		user.getRoleIds().remove(oldRole.getId());
		operations.save(user);
		
		operations.remove(oldRole);

		return true;
	}

	@Override
	public boolean changeRoleExpired(String userId, String roleName, LocalDate expired) {
		// TODO Auto-generated method stub
		Role old = roleService.findRoleByUserIdAndName(userId, roleName);
		if (old == null) {
			return false;
		}
		old.setExpiredDate(expired);
		operations.save(old);
		
		return true;
	}


	@Override
	public User updateUser(String userId, Map<String, Object> map) throws NoSuchFieldException {
		Update update=new Update();
		for(Map.Entry<String,Object> entry:map.entrySet()){
			switch (entry.getKey()) {
				case "roleIds" -> {
					log.warn("can not update roleIds field");
					continue;
				}
				case "username" -> {
					log.warn("can not update username ");
					continue;
				}
				case "id" -> {
					log.warn("can not update id field");
					continue;
				}
				case "password" -> {
					log.warn("can not update password field");
					continue;
				}
				case "phone" -> {
					log.warn("can not update phone field");
					continue;
				}
				case "email" -> {
					log.warn("can not update email field");
					continue;
				}
			}
			update.set(entry.getKey(),entry.getValue());
		}
        operations.updateFirst(Query.query(Criteria.where("id").is(userId)),update,User.class);
		return operations.findById(userId,User.class);
	}

}
